package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.TechnicianOrderItem;
import com.hanhanxiaochong.zhongxiangchuanmei.config.page.PageRecorder;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.TechnicianOrderPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.ITechOrderView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.TechOrderAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/10.
 * Desc   :员工订单管理
 */

public class AlertOrderActivity extends BaseActivity<TechnicianOrderPresenter> implements ITechOrderView {


    @BindView(R.id.txt_left)
    TextView txtLeft;
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.ll_leftview)
    RelativeLayout llLeftview;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.ll_rightview)
    RelativeLayout llRightview;
    @BindView(R.id.view_bottomline)
    View viewBottomline;
    @BindView(R.id.rl_topview)
    RelativeLayout rlTopview;
    @BindView(R.id.recyclerview_alertorder)
    RecyclerView recyclerviewAlertorder;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;

    private PageRecorder pageRecorder;
    private TechOrderAdapter techOrderAdapter;
    private String token;
    private String checkTime;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_alert_order);
        pageRecorder = new PageRecorder();
        token = SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken();
    }

    @Override
    protected void initPresenter() {
        mPresenter = new TechnicianOrderPresenter(this);
    }

    @Override
    protected void init() {
        imgLeft.setImageResource(R.mipmap.icon_left);
        txtTitle.setText("订单管理");
        imgRight.setImageResource(R.mipmap.icon_calendar);

        List<TechnicianOrderItem> list = new ArrayList<>();
        techOrderAdapter = new TechOrderAdapter(this, R.layout.item_tech_order, list);
        recyclerviewAlertorder.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerviewAlertorder.setAdapter(techOrderAdapter);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        checkTime = sdf.format(calendar.getTime());
        mPresenter.getOrderList(token, checkTime, pageRecorder.getCurrentPage());

        smartRefresh.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.getOrderList(token, checkTime, pageRecorder.getCurrentPage());
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageRecorder.moveToFirstPage();
                mPresenter.getOrderList(token, checkTime, pageRecorder.getCurrentPage());
            }
        });

    }

    @OnClick({R.id.ll_leftview, R.id.ll_rightview})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.ll_rightview:
                TimePickerView pickerView = new TimePickerView.Builder(this, (date, v) -> {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(date.getTime());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    checkTime = sdf.format(calendar.getTime());
                    pageRecorder.moveToFirstPage();
                    mPresenter.getOrderList(token, checkTime, pageRecorder.getCurrentPage());
                }).setType(new boolean[]{true, true, true, false, false, false})
                        .setCancelText("取消")
                        .setSubmitText("确定")
                        .build();
                pickerView.show();
                break;
        }
    }

    @Override
    public void setOrderList(List<TechnicianOrderItem> data) {
        smartRefresh.finishRefresh();
        pageRecorder.moveToNextPage();
        techOrderAdapter.setDatas(data);
    }

    @Override
    public void addOrderList(List<TechnicianOrderItem> data) {
        smartRefresh.finishLoadmore();
        pageRecorder.moveToNextPage();
        techOrderAdapter.addDatas(data);
    }
}
